package com.cndw.kungfu.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.stereotype.Component;

import com.cndw.kungfu.core.CacheDataBase;
import com.cndw.kungfu.core.CacheDataKey;
import com.cndw.kungfu.model.pve.UnionPetBattle;
import com.cndw.kungfu.model.pvp.ChallengeData;
import com.cndw.kungfu.model.undercity.RankingContainer;

/**
 * 持续化到硬盘的临时存储信息
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-6-2 下午02:44:05
 */
@Component
public class DataSerialize extends CacheDataBase implements Serializable {

	private static final long serialVersionUID = 1L;

	private static DataSerialize instance = null;
	private Map<Integer, Integer> numMap = new HashMap<Integer, Integer>();
	
	private UnionPetBattle unionPetBattle;

	/**
	 * 帮派申请列表
	 */
	private transient Map<Integer, Map<Long, Integer>> applyMember = new HashMap<Integer, Map<Long, Integer>>();

	/**
	 * 通天图、神兵阵排行
	 */
	private Map<Integer, RankingContainer> recordRanking = new HashMap<Integer, RankingContainer>();

	/**
	 * 排行数据
	 */
	private Map<Integer, Map<Integer, Long>> rankingPlayer = new ConcurrentHashMap<Integer, Map<Integer, Long>>();

	private DataSerialize() {

	}

	public static DataSerialize getInstance() {
		if (instance == null) {
			synchronized (DataSerialize.class) {
				if (instance == null) {
					CacheDataBase cacheData = Context.getSysServer().initCacheData(CacheDataKey.keyDataSerialze);
					if (cacheData != null && cacheData instanceof DataSerialize) {
						instance = (DataSerialize) cacheData;
					}
					if (instance == null) {
						instance = new DataSerialize();
					}
					instance.applyMember = new HashMap<Integer, Map<Long, Integer>>();
					if (instance.getSyncDBTime() == 0) {
						instance.commitSync();
					}
				}
			}
		}
		return instance;
	}

	public Map<Integer, Integer> getNumMap() {
		return numMap;
	}

	public Map<Integer, Map<Long, Integer>> getApplyMember() {
		return applyMember;
	}

	public Map<Integer, RankingContainer> getPassThoughRanking() {
		return recordRanking;
	}
	/**
	 * 数据转移出去了
	 * @return
	 */
	public ChallengeData getChallengeData() {
		return PvpChallengeSerialize.getInstance().getChallengeData();
	}

	public Map<Integer, Map<Integer, Long>> getRankingPlayer() {
		if (rankingPlayer == null) {
			synchronized (this) {
				if (rankingPlayer == null) {
					rankingPlayer = new ConcurrentHashMap<Integer, Map<Integer, Long>>();
				}
			}
		}
		return rankingPlayer;
	}

	/**
	 * 根据称号类型获取排行玩家id
	 */
	public Map<Integer, Long> getRanking(int titleType) {
		if (getRankingPlayer().containsKey(titleType)) {
			return getRankingPlayer().get(titleType);
		} else {
			return null;
		}
	}

	/**
	 * 修改排行
	 */
	public synchronized void changeRanking(int titleType, Map<Integer, Long> newRanking) {
		if (!getRankingPlayer().containsKey(titleType)) {
			getRankingPlayer().put(titleType, new ConcurrentHashMap<Integer, Long>());
		}
		getRankingPlayer().get(titleType).clear();
		getRankingPlayer().get(titleType).putAll(newRanking);
	}
	
	public UnionPetBattle getUnionPetBattle() {
		if (unionPetBattle == null) {
			synchronized (DataSerialize.class) {
				if (unionPetBattle == null) {
					unionPetBattle = new UnionPetBattle();
				}
			}
		}
		return unionPetBattle;
	}

	@Override
	public String getKey() {
		return CacheDataKey.keyDataSerialze;
	}
}
